﻿namespace C__study_2_24
{
    public class Arr
    {
        private int capcity;
        private int[] arr ;
        private int length;
        public  Arr()
        {
            capcity = 4;
            arr = new int[capcity];
            length = 0;
        }
        public void AddArr(int value)
        {
            if (length < capcity)
            {
                arr[length++] = value;
            }
            else
            {
                capcity *= 2;
                int[] newarr = new int[capcity];
                for(int i=0;i<length;i++)
                {
                    newarr[i] = arr[i];
                }
                newarr[length++] = value;
                arr = newarr;
            }
        }
        public int  FInd(int value)
        {
            if (length > capcity)
            {
                Console.WriteLine("越界了");
                return -1;
            }
            for (int i = 0; i < length; i++)
            {
                if (arr[i] == value)
                {
                    Console.WriteLine("找到了，是{0}", i);
                    return i;
                }
            }
            
             Console.WriteLine("没找到");
             return -1;
            
        }
        public void Delet(int value)
        {
            int i= FInd(value);
            if(i == -1)
            return;
            else
            {
                for(int j= i; j < length-1; j++)
                {
                    arr[j] = arr[j + 1];
                }
                length--;
            }
        }

        public int this[int index]
        {
            get
            {
                return arr[index];
            }
            set
            {
                if (value < 0)
                {
                    value = 0;
                    Console.WriteLine("请输入一个大于零的数");
                }
                else if (value >= length)
                {
                    value= length-1;
                    Console.WriteLine("越界了");
                }
                index = value;
            }
        }

        public int Length
        {
            get
            {
                return length;
            }
        }
    }
        
        internal class Program
    {
        //索引器
      
        static void Main(string[] args)
        {
            Arr arr = new Arr();
            arr.AddArr(2);
            arr.AddArr(3);
            arr.AddArr(2);
            arr.AddArr(2);
            arr.AddArr(2);
            arr.AddArr(6);
            Console.WriteLine("{0}", arr.Length);
        }
    }
}
